package com.jianguozh.difs.framework.security.service;

import cn.hutool.db.Entity;
import com.jianguozh.difs.framework.constant.enums.ClientEnum;
import com.jianguozh.difs.framework.constant.enums.OnOffEnum;
import com.jianguozh.difs.framework.security.AuthenticatedUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 用户详情服务
 *
 * @author hanrs
 * @email 4076130@qq.com
 * @since 2022/3/10 14:24
 */
@Slf4j
@Service
public class DifsUserDetailsService implements UserDetailsService {

    @Autowired
    DifsUserDetailsDao difsUserDetailsDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Entity entity = difsUserDetailsDao.loadUserByUsername(username);
        if (null != entity) {
            List<Entity> entities = difsUserDetailsDao.selecListUserClient(username);
            Set<String> authorityTags = new HashSet<>();
            entities.forEach(e -> {
                String appId = e.getStr("app_id");
                ClientEnum clientEnum = ClientEnum.fromAppId(appId);
                if(null != clientEnum){
                    authorityTags.add(clientEnum.getAuthorityTag());
                }
            });
            return createAuthenticatedUser(entity, authorityTags);
        } else {
            throw new UsernameNotFoundException(username + "不存在.");
        }
    }


    private AuthenticatedUser createAuthenticatedUser(Entity entity, Set<String> authorityTags) {
        AuthenticatedUser authenticatedUser = new AuthenticatedUser();
        authenticatedUser.setId(entity.getStr("id"));
        authenticatedUser.setOrg(entity.getStr("org"));
        authenticatedUser.setOrgId(entity.getStr("org_id"));
        authenticatedUser.setOrgDefault(entity.getBool("org_default"));
        authenticatedUser.setUsername(entity.getStr("username"));
        authenticatedUser.setPassword(entity.getStr("password"));
        authenticatedUser.setName(entity.getStr("name"));
        authenticatedUser.setAvatar(entity.getStr("avatar"));
        authenticatedUser.setStatus(entity.getStr("status").equalsIgnoreCase(OnOffEnum.ON.getValue()));
        authenticatedUser.setPwdInited(entity.getBool("pwd_inited"));
        authenticatedUser.setLocked(entity.getBool("locked"));
        Set<GrantedAuthority> authorities = new HashSet<>();
        authorityTags.forEach(o -> {
            authorities.add(new SimpleGrantedAuthority(o));
        });
        authenticatedUser.setAuthorities(authorities);
        return authenticatedUser;
    }

}
